'use strict';
/** 桌子消息 */
module.exports.run = function(ud){
    const bs = require("../module/base"), room = require("../config/room.json");
    let d = ud.json;
    switch(ud.S){
        case 1:{ //坐下
            if(d.length > 4) return;
            let tid = d.readUInt16LE();

            if(s.U.R) return bs.sendE(s, 6); //当前是否有房间
            let rm = room[d[2]][0], i=0;
            for(;i<rm.length;i++){
                if(rm[i][0] == d[3]) break;
            }
            if(i == rm.length) bs.sendE(s, 7); 
            //寻找桌子
            let tbl = TABLE[d[3]];
            tbl || (tbl = TABLE[d[3]] = []);
            if(d.length == 4){ //服务器找桌子
                i = 0;
                for(;i<room[d[2]][3];i++){
                    tbl[i] || (tbl[i] = []);
                    for(let j=0;j<room[d[2]][4];j++){
                        if(!tbl[i][j]){ //当前位置空的
                            this.setGameTable(s, 2, i, j, d[2], d[3]);
                            tbl[i][j] = s;
                            break;
                        }
                    }
                    if(s.U.R) break;
                }
            }else{ //制定桌子坐下
                if(d[4] > -1 && d[4] < room[d[2]][3]){
                    tbl[d[4]] || (tbl[d[4]] = []);
                    for(let j=0;j<room[d[2]][4];j++){
                        if(!tbl[i][j] && (d[5] == j || !d[5])){ //当前位置空的
                            this.setGameTable(s, 2, d[4], j, d[2], d[3]);
                            tbl[d[4]][j] = s;
                            break;
                        }
                    }
                }else{
                    return bs.sendE(s, 8);
                }
            }
            break;
        }case 3:{ //起立
            break;
        }case 4:{ //强退桌子
            break;
        }case 5:{ //同步桌子
            break;
        }case 8:{ //传送IP
            if(ud.U.base.index != ud.index){ //关闭旧连接
                const cl = require("./close");
                cl.sendClose(ud.U);
            }
            if(!(ud.userid in USER)) USER[ud.userid] = {IP:""};
            USER[ud.userid].IP = d.readUInt8(8)+"."+d.readUInt8(9)+"."+d.readUInt8(10)+"."+d.readUInt8(11);
            USER[ud.userid].base = {index:ud.index,userid:ud.userid,channelid:ud.channelid};
            break;
        }case 9:{ //加载用户
            //房间是否关闭判断


            if(ud.U.info){ //用户已经登陆
                sendUserItem(ud.base, ud.U.info);
                break;
            }
            //加载用户信息
            bs.MDB.executeSql(`EXEC [QPTreasureDB].[dbo].[TC_LoadUser] ${ud.userid}, ${KINDID}, ${SERVERID}`, function(rows){
                if(rows.FaceID){
                    //进入房间条件检测

                    ud.U.info = {wFaceID:rows.FaceID[0],dwUserID:ud.userid,dwGameID:rows.GameID[0],dwUserRight:rows.UserRight[0],NickName:rows.NickName[0],cbGender:rows.Gender[0],wTableID:65535,wChairID:65535,cbUserStatus:1,Score:Number(rows.Score[0]),szUnderWrite:rows.UnderWrite[0]};
                    sendUserItem(ud.base, ud.U.info);
                    if(rows.ret[0] == 0 && rows.android[0] == 0){
                        rd(2).HMSET("UC:"+ud.userid, "K", KINDID, "S", SERVERID);
                        loadKill(KINDID, SERVERID);
                    } 
                }
            })
            break;
        }
    }
}

/** 发送用户信息 */
function sendUserItem(ud, u){
    let d = [8,100,{F:u.wFaceID,U:u.dwUserID,E:u.dwGameID,G:u.cbGender,T:u.wTableID,C:u.wChairID,S:u.cbUserStatus,M:u.Score,A:u.NickName,underwrite:u.szUnderWrite}];
    bs.send(ud, d);
}

/** 设置游戏桌子 userStatus 1起立 2坐下 3准备 4离线 5游戏 */
module.exports.setGameTable = function(s, userStatus, tid, cid){
    let us;
    if(userStatus == 2){ //设置坐下状态
        s.U.R = {K:KindId,S:serverId,T:tid,C:cid,B:2};
        us = [8,0,s.U.R.T,s.U.R.C,s.U.R.B,s.U.uid,s.U.S]
    }else{
        s.U.R.B = userStatus;
        us = [8,1,s.U.R.C,s.U.R.B,s.U.S]
    }

    let tb = TABLE[serverId][s.U.R.T];
    for(let i=0;i<tb.length;i++){
        if(tb[i]){
            let u = tbl[i].U;
            if(userStatus == 2){
                bs.send(s, [8,0,s.U.R.T,u.R.C,u.R.B,u.uid,u.S]);
                if(tb[i] != s) bs.send(tb[i], us);
            }else{
                bs.send(s, us);
            }
        }
    }
    //状态1特殊处理
    if(userStatus == 1){
        tb[s.U.R.C] = null;
        delete s.U.R;
    } 
}